package xyj.sparrow.scheduler.conf;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class SparrowSnowflake {
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private long workerId;//为终端ID
    private long datacenterId = 1;//数据中心ID
    private Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenterId);
    @Value("${xyj.sparrow.server.port}")
    private int port;

    @PostConstruct
    public void init() {
        try {
            workerId = Long.valueOf(NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()) + "" + port);
        } catch (Exception e) {
            log.error("", e);
            workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
        }
        log.info("当前机器的workId:{}", workerId);
    }

    public synchronized long snowflakeId() {
        return snowflake.nextId();
    }

    public synchronized long snowflakeId(long workerId, long datacenterId) {
        Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenterId);
        return snowflake.nextId();
    }
}